﻿using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Text;

namespace TbDataSync
{
    public partial class Program
    {

        /// <summary>
        /// 统一登录新老系统
        /// </summary>
        public static void Login()
        {
            LoginOld();
            LoginNew();
        }

        /// <summary>
        /// 登录源系统
        /// </summary>
        public static void LoginOld()
        {
            //X-Authorization
            var data = fromIdentity;
            var content = new StringContent(JsonConvert.SerializeObject(data), Encoding.UTF8, "application/json");

            old_client.DefaultRequestHeaders.Clear();
            old_client.DefaultRequestHeaders.Accept.Clear();
            old_client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));

            var result = old_client.PostAsync(urls["login"], content).Result.Content.ReadAsStringAsync().Result;

            var res = JsonConvert.DeserializeObject<LoginResult>(result);
            old_client.DefaultRequestHeaders.Add("X-Authorization", $"Bearer {res.token}");
        }

        /// <summary>
        /// 登录新系统
        /// </summary>
        public static void LoginNew()
        {
            //X-Authorization
            var data = targetIdentity;
            var content = new StringContent(JsonConvert.SerializeObject(data), Encoding.UTF8, "application/json");

            new_client.DefaultRequestHeaders.Clear();
            new_client.DefaultRequestHeaders.Accept.Clear();
            new_client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));

            var result = new_client.PostAsync(urls["login"], content).Result.Content.ReadAsStringAsync().Result;

            var res = JsonConvert.DeserializeObject<LoginResult>(result);
            new_client.DefaultRequestHeaders.Add("X-Authorization", $"Bearer { res.token}");
        }

    }
}
